/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.MembershipManager;

import utilities.DBConnector;
import resource.Macro;
import resource.Member;
import utilities.Codecs;
import utilities.Helper;
import utilities.Prompt;

/**
 *
 * @author jiaf
 */
public class MembershipManager extends MembershipManagerBase {
    /**
     * Creates new form MembershipManager
     * @param dbSession
     */
    public MembershipManager(DBConnector dbSession) {
        super(dbSession);
        initComponents();
        setTargetTable(membershipTable);
        refresh();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        membershipTable = new javax.swing.JTable();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(980, 550));
        setResizable(false);

        jButton1.setText("Add");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Edit");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Delete");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Refresh");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        membershipTable.setAutoCreateRowSorter(true);
        membershipTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Card No.", "Name", "Group", "Gender", "Date of Birth", "Registration Date", "Expiry Date", "Contact Address", "Phone (H)", "Phone (O)", "Phone (M)", "E-mail", "Paid", "id"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        membershipTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(membershipTable);

        jButton5.setText("Cancel");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 928, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton4)
                        .add(52, 52, 52)
                        .add(jButton3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton5)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
        this.dispose();
    }//GEN-LAST:event_jButton5MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        MM_Add mm_add = new MM_Add(getDBSession(), membershipTable);
        mm_add.setVisible(true);
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        refresh();
    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        int row_num = membershipTable.getSelectedRow();
        row_num = membershipTable.convertRowIndexToModel(row_num);
        if (row_num != -1){
            Member member = new Member(
                membershipTable.getModel().getValueAt(row_num, 0).toString(),
                membershipTable.getModel().getValueAt(row_num, 1).toString(),
                membershipTable.getModel().getValueAt(row_num, 2).toString(),
                membershipTable.getModel().getValueAt(row_num, 3).toString(),
                membershipTable.getModel().getValueAt(row_num, 4).toString(),
                membershipTable.getModel().getValueAt(row_num, 5).toString(),
                membershipTable.getModel().getValueAt(row_num, 6).toString(),
                membershipTable.getModel().getValueAt(row_num, 7).toString(),
                membershipTable.getModel().getValueAt(row_num, 8).toString(),
                membershipTable.getModel().getValueAt(row_num, 9).toString(),
                membershipTable.getModel().getValueAt(row_num, 10).toString(),
                membershipTable.getModel().getValueAt(row_num, 11).toString(),
                membershipTable.getModel().getValueAt(row_num, 12).toString(),
                membershipTable.getModel().getValueAt(row_num, 13).toString()
            );
            MM_Edit mm_edit = new MM_Edit(getDBSession(), member,
                    membershipTable);
            mm_edit.setVisible(true);
        }
        else{
            Prompt.warning("No data selected");
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        int row_num = getTargetTable().getSelectedRow();
        row_num = getTargetTable().convertRowIndexToModel(row_num);
        if (row_num != -1){
            String _name = getTargetTable().getModel().getValueAt(row_num, 1).
                    toString();
            String _id = getTargetTable().getModel().getValueAt(row_num, 13).
                    toString();
            if (Helper.confirmDelete(this, _name) == 0) {
                getDBSession().delete(Macro.DB_MEMBERSHIP,
                        String.format("%s='%s'", Macro.MEMBERSHIP_ID_L,
                                Codecs.encode(_id)));
                refresh();
            }
        }
        else{
            Prompt.warning("No data selected");
        }
    }//GEN-LAST:event_jButton3MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable membershipTable;
    // End of variables declaration//GEN-END:variables
}
